package com.example.moonshine.redchange.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;


import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.moonshine.redchange.Fragment.FirstlListFragment;
import com.example.moonshine.redchange.Fragment.SecondListFragment;

import com.example.moonshine.redchange.R;
import com.github.zackratos.ultimatebar.UltimateBar;


@RequiresApi(api = Build.VERSION_CODES.M)
public class MaintwoActivity extends AppCompatActivity implements View.OnClickListener {


    private FirstlListFragment change1;
    private SecondListFragment change2;
    // 帧布局对象，用来存放Fragment对象
    private FrameLayout frameLayout;
    // 定义每个选项中的相关控件
    private RelativeLayout firstLayout;
    private RelativeLayout secondLayout;
    private RelativeLayout thirdLayout;
    private TextView firstText;
    private TextView secondText;
    private TextView thirdText;
    String resultIndex;
    // 定义几个颜色
    private int whirt = 0xFFFFFFFF;
    private int gray = 0xFF7597B3;
    private int dark = 0xff000000;
    // 定义FragmentManager对象管理器
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listmain_activity);
        UltimateBar.newColorBuilder()
                .statusColor(Color.parseColor("#ffffff"))       // 状态栏颜色
                .statusDepth(50)                // 状态栏颜色深度
                .build(this)
                .apply();
        fragmentManager = getSupportFragmentManager();
        initView(); // 初始化界面控件
        resultIndex = getIntent().getStringExtra("index");
        if (resultIndex!=null) {
            setChioceItem(Integer.parseInt(resultIndex));
        }
        else {
            setChioceItem(0); // 初始化页面加载时显示第一个选项卡
//            setUser();
        }
    }
    /**
     * 初始化页面
     */
    @SuppressLint("CutPasteId")
    private void initView() {
// 初始化底部导航栏的控件
        firstText = (TextView) findViewById(R.id.first_text);
        secondText = (TextView) findViewById(R.id.second_text);
        firstLayout = (RelativeLayout) findViewById(R.id.first_layout1);
        secondLayout = (RelativeLayout) findViewById(R.id.second_layout1);
        secondLayout.setOnClickListener(MaintwoActivity.this);
        firstLayout.setOnClickListener(MaintwoActivity.this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.first_layout1:
                setChioceItem(0);
                break;
            case R.id.second_layout1:
                setChioceItem(1);
                break;
            default:
                break;
        }
    }

    /**
     * 设置点击选项卡的事件处理
     *
     * @param index 选项卡的标号：0, 1, 2
     */
    private void setChioceItem(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        clearChioce(); // 清空, 重置选项, 隐藏所有Fragment
        hideFragments(fragmentTransaction);
        switch (index) {
            case 0:
// firstImage.setImageResource(R.drawable.XXXX); 需要的话自行修改
                firstText.setTextColor(gray);
                firstLayout.setBackgroundColor(whirt);

// 如果fg1为空，则创建一个并添加到界面上
                if (change1 == null) {
                    change1 = new FirstlListFragment();
                    fragmentTransaction.add(R.id.content, change1);
                } else {
// 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(change1);
                }
                break;
            case 1:
// secondImage.setImageResource(R.drawable.XXXX);
                secondText.setTextColor(gray);
                secondLayout.setBackgroundColor(whirt);
                if (change2 == null) {
                    change2 = new SecondListFragment();
                    fragmentTransaction.add(R.id.content, change2);
                } else {
                    fragmentTransaction.show(change2);
                }
                break;
//            case 2:
//// thirdImage.setImageResource(R.drawable.XXXX);
//                thirdText.setTextColor(dark);
//                thirdLayout.setBackgroundColor(gray);
//                if (change3 == null) {
//                    change3 = new ThirdListFragment();
//                    fragmentTransaction.add(R.id.content, change3);
//                } else {
//                    fragmentTransaction.show(change3);
//                }
//                break;
// fourthImage.setImageResource(R.drawable.XXXX);

        }
        fragmentTransaction.commit(); // 提交
    }

    /**
     * 当选中其中一个选项卡时，其他选项卡重置为默认
     */
    private void clearChioce() {
// firstImage.setImageResource(R.drawable.XXX);
        firstText.setTextColor(dark);
        firstLayout.setBackgroundColor(whirt);
// secondImage.setImageResource(R.drawable.XXX);
        secondText.setTextColor(dark);
        secondLayout.setBackgroundColor(whirt);
// thirdImage.setImageResource(R.drawable.XXX);
//        thirdText.setTextColor(gray);
//        thirdLayout.setBackgroundColor(whirt);
// fourthImage.setImageResource(R.drawable.XXX);
    }

    /**
     * 隐藏Fragment
     *
     * @param fragmentTransaction
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        if (change1 != null) {
            fragmentTransaction.hide(change1);
        }
        if (change2 != null) {
            fragmentTransaction.hide(change2);
        }
//        if (change3 != null) {
//            fragmentTransaction.hide(change3);
//        }
    }


}

